package com.peng.edu.service.impl;

import com.peng.base.exception.GuliException;
import com.peng.edu.client.UserClient;
import com.peng.edu.entity.Comment;
import com.peng.edu.entity.user.UcenterMember;
import com.peng.edu.mapper.CommentMapper;
import com.peng.edu.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peng.utils.JwtUtils;
import com.peng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author Dengp
 * @since 2022-06-24
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private UserClient userClient;

    //添加评论
    @Override
    public void addComment(Comment comment, HttpServletRequest request) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        if (userId==null){
            throw new GuliException(20001,"请先登录");
        }
        UcenterMember userInfo = userClient.getUserInfoClient(userId);
        Comment comment1 = new Comment();
        comment1.setCourseId(comment.getCourseId());
        comment1.setTeacherId(comment.getTeacherId());
        comment1.setContent(comment.getContent());
        comment1.setMemberId(userInfo.getId());
        comment1.setNickname(userInfo.getNickname());
        comment1.setAvatar(userInfo.getAvatar());
        this.save(comment1);
    }
}
